{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 冬油菜影像氮养分场景分类  \n",
    "\n",
    "本项目使用无人机航拍冬油菜影像，判断油菜养分胁迫状况。  \n",
    "（第一次写项目没啥经验，还请大家多多批评指正）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 一、项目背景\n",
    "\n",
    "起初只是为了判断是否油菜受到氮养分胁迫，但为了更加便捷运用数字图像处理的知识，完成了这一部分内容。  \n",
    " \n",
    "**下面是结果展示：**  可以看到下面的这个结果非常类似于目标检测，从而获取图像的bbox，实际上我做的部分只是单纯的分类。  \n",
    "* 原因在于没有采集到大量的整张图片数据，训练效果较差，所以做了手动bbox。  \n",
    "\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/ca7fdefb74cb41b2baa7c336af1140842d965556d9544dad9bfbbb4ddda987ce)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 二、数据集简介\n",
    "\n",
    "* 数据展示如下，大家可以看见，油菜的图片为什么是**红色**的？   \n",
    "* 实际上，这就是**遥感影像**的特点，遥感影像通常为多个波段，对遥感影像有过了解的同学就知道，高光谱影像甚至有上百个波段；  \n",
    "* 我们所使用的无人机影像有4个波段，为了能让计算机显示出4个波段的图像，这里我将近红外NIR波段赋予红色，红波段赋予绿色，绿波段赋予蓝色；  \n",
    "* 结果就如下所示，是不是更能区分**土壤与植物**了？没错，这就是假彩色与多波段的结果，更多的波段导致了**更全面的图像信息**。  \n",
    "* 我们这次使用的影像为‘**.npy**’格式的图像，因为原始图像为‘**.tif**’格式的，需要用“gdal”库打开，我把它转为numpy格式了，方便读取。  \n",
    "  \n",
    "  ![](https://ai-studio-static-online.cdn.bcebos.com/f530b3e10a8941679e8900ba239f1ba98e9bda57e25a494a910c5aed7e8cbbe3)\n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 1.数据加载和预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 先解压文件\r\n",
    "#! unzip -oq data/data98048/ttst.zip\r\n",
    "#! unzip -oq data/data98048/paddlex.zip"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "* 对文件进行解压之后，会出现ttst文件与paddlex套件，为什么这里写的是小写呢？  \n",
    "\n",
    "* 因为为了适合于遥感影像分类，我将PaddleX进行了一些修改，因为目前PaddleX分类是基于ImageNet的图像分类。\n",
    "* **修改内容如下**：（本项目文件已经修改了，大家也可以自行下载新版本，自己修改）  \n",
    "1.使用paddlex cv datasets 文件中的 dataset.py 中 valid_suffix = ['JPEG', 'jpeg', 'JPG', 'jpg', 'BMP', 'bmp', 'PNG', 'png', 'npy']  \n",
    "也就是添加了‘.npy’文件格式，在PaddleX分割套件中支撑npy格式，不知道为啥分类就不支持了。  \n",
    "2.cv中的 transforms的文件中的 cls——transforms修改如下：  \n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/694d2f2407af448daa08e8aa9830d759f457b0f635b2478ab3b47d5fa6603b8a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[WARNING] pycocotools is not installed, detection model is not available now.\n",
      "[WARNING] pycocotools install: https://paddlex.readthedocs.io/zh_CN/develop/install.html#pycocotools\n"
     ]
    }
   ],
   "source": [
    "#如果是解压了的文件的话，要将小写paddlex 修改为 正常的 PaddleX     提醒自己\r\n",
    "import sys\r\n",
    "sys.path.append('PaddleX')\r\n",
    "import os\r\n",
    "import paddle\r\n",
    "import numpy as np\r\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = '0'\r\n",
    "import paddlex as pdx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "* 那咱们看一下数据，这是ttst文件夹里面的数据。  \n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/64bfde4a78c343a8970317f5d4844286c175d1ad4999490084addece67b0a833)  \n",
    "* 那看一下npy格式的图像数据吧： （是不是很奇怪？） \n",
    "* 这张图片是40 * 40的图片，因为我是对一块农田进行了 **裁剪**。  \n",
    "* **题外话：** 农田图像并不具备RGB里面的形状特征，例如把猫或者车辆切成两半的预测效果肯定是不好的，但是农田可以。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAHEdJREFUeJztnduOJDmanI2kHyIiD3Xorj7MaAe7Wugp9Qh6j73SA0mAVqOdnUN3dVdVZmVmHNyd5F7MncwI5GiBWEC075LJ8KAz/E8HjEZjqLXCGNMf8T96AMaY/xhc/MZ0iovfmE5x8RvTKS5+YzrFxW9Mp7j4jekUF78xneLiN6ZThmt+2T/+138iO+E2v5d913Gjtrr8b33hP5+oafvLs+waHi/ctibZd0g8PcNuz+Oa9D1syNy44/sCAHzD/4e3Gz0ulP/J4zpxGwCEL0/UVj//qC+7/R217eZ31HZI+p0RIcZ7uNXj+u1Mbafvz7LvY/1IbeeHB9l3e+KxhZeJ2tLXxj088LjS0w+y7yHcU9ubSY9rrj9R2xL4WQSA4w8jtdX/zM84AMzpj9T2P/7bfw+y8/+F3/zGdIqL35hOcfEb0ykufmM65aqC32FkQWh71trEJa/Ulge9/bjsWAyJ7x5l3xqFqLRqUSoOYmyRRZqt/io/f6k7/vj0Rn/XWKitbovsOz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hjDUYiDz0lf9yIShF+L3/zGdIqL35hOcfEb0ykufmM6xcVvTKdcVe2/TKz216jV4Aksiw9RW3bDzEpovdVj2G5YHT2J9F8AwIXHoMItwtJIwxVn2pWsVxYuIuRjUF5V6ITZ2gj+WES4xdrIgg2qXVicG2G2qJXnKzcU/CzmsW56bpJoHxpnPCY1j7NIbG6MaxBn6iG17Mzi2W2EeYSdWDpqJBtXkdqcG2r/IqzPr8VvfmM6xcVvTKe4+I3pFBe/MZ0SWnuzjTH/f+M3vzGd4uI3plNc/MZ0iovfmE5x8RvTKS5+YzrFxW9Mp7j4jekUF78xneLiN6ZTXPzGdIqL35hOcfEb0ykufmM6xcVvTKe4+I3pFBe/MZ3i4jemU1z8xnSKi9+YTnHxG9MpLn5jOsXFb0yn/BuOW8WmM2+AAwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 看看数据\r\n",
    "import matplotlib.pyplot as plt\r\n",
    "%matplotlib inline\r\n",
    "import warnings\r\n",
    "warnings.filterwarnings(\"ignore\")\r\n",
    "img = np.load('ttst/N1_NPY/N1_11_24.npy')\r\n",
    "plt.imshow(img[:,:,1:])\r\n",
    "plt.axis('off')\r\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2.数据集查看  \n",
    "* 这里首先定义了transforms，其中主要的就是将图像变为224* 224的尺寸以及水平与竖直翻转；  \n",
    "* 因为归一化在拿到数据的时候就处理了，再做一次其实也行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from paddlex.cls import transforms\r\n",
    "train_transforms = transforms.Compose([\r\n",
    "    transforms.ResizeByShort(short_size=224),\r\n",
    "    transforms.RandomHorizontalFlip(),\r\n",
    "    transforms.RandomVerticalFlip()\r\n",
    "])\r\n",
    "eval_transforms = transforms.Compose([\r\n",
    "    transforms.ResizeByShort(short_size=224)\r\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-08-14 17:55:15 [INFO]\tStarting to read file list from dataset...\n",
      "2021-08-14 17:55:15 [INFO]\t1242 samples in file ttst/train_set.txt\n",
      "2021-08-14 17:55:15 [INFO]\tStarting to read file list from dataset...\n",
      "2021-08-14 17:55:15 [INFO]\t138 samples in file ttst/eval_set.txt\n"
     ]
    }
   ],
   "source": [
    "train_dataset = pdx.datasets.ImageNet(\r\n",
    "    data_dir='ttst',\r\n",
    "    file_list='ttst/train_set.txt',\r\n",
    "    label_list='ttst/labels.txt',\r\n",
    "    transforms=train_transforms,\r\n",
    "    shuffle=True)\r\n",
    "eval_dataset = pdx.datasets.ImageNet(\r\n",
    "    data_dir='ttst',\r\n",
    "    file_list='ttst/eval_set.txt',\r\n",
    "    label_list='ttst/eval_set.txt',\r\n",
    "    transforms=eval_transforms)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 三、模型选择和开发\n",
    "\n",
    "* 因为使用的是PaddleX套件进行训练，这里我选择了很多模型进行一个对比，因为数据量可能还是有点少，出现**过拟合**了。  \n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/f1f61cf4719c4966befee0a981ba4e8a9265db4829124c249572239837b76384)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "num_classes = len(train_dataset.labels)\r\n",
    "model = pdx.cls.DenseNet161(num_classes=num_classes,input_channel=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 3.模型训练\n",
    "* 这里只跑了1个epochs，在GPU环境下，CPU好像会提升内存溢出。  \n",
    "\n",
    "* 模型选择为DenseNet：  \n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/3135ac0dec174231a0b123a3805de6bdbad8bcaabc1e4cfbb3912660e2f07816)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/framework.py:706: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  elif dtype == np.bool:\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-08-14 17:55:35 [INFO]\t[TRAIN] Epoch=1/1, Step=2/38, loss=1.062336, acc1=0.40625, acc2=1.0, lr=0.025, time_each_step=2.85s, eta=0:1:56\n",
      "2021-08-14 17:55:36 [INFO]\t[TRAIN] Epoch=1/1, Step=4/38, loss=0.420335, acc1=0.8125, acc2=1.0, lr=0.025, time_each_step=1.56s, eta=0:1:0\n",
      "2021-08-14 17:55:36 [INFO]\t[TRAIN] Epoch=1/1, Step=6/38, loss=0.365954, acc1=0.8125, acc2=1.0, lr=0.025, time_each_step=1.13s, eta=0:0:41\n",
      "2021-08-14 17:55:37 [INFO]\t[TRAIN] Epoch=1/1, Step=8/38, loss=0.578288, acc1=0.75, acc2=1.0, lr=0.025, time_each_step=0.92s, eta=0:0:32\n",
      "2021-08-14 17:55:38 [INFO]\t[TRAIN] Epoch=1/1, Step=10/38, loss=0.391463, acc1=0.8125, acc2=1.0, lr=0.025, time_each_step=0.79s, eta=0:0:26\n",
      "2021-08-14 17:55:38 [INFO]\t[TRAIN] Epoch=1/1, Step=12/38, loss=0.681714, acc1=0.84375, acc2=1.0, lr=0.025, time_each_step=0.7s, eta=0:0:21\n",
      "2021-08-14 17:55:39 [INFO]\t[TRAIN] Epoch=1/1, Step=14/38, loss=0.807979, acc1=0.84375, acc2=1.0, lr=0.025, time_each_step=0.64s, eta=0:0:18\n",
      "2021-08-14 17:55:39 [INFO]\t[TRAIN] Epoch=1/1, Step=16/38, loss=0.93035, acc1=0.71875, acc2=1.0, lr=0.025, time_each_step=0.59s, eta=0:0:15\n",
      "2021-08-14 17:55:40 [INFO]\t[TRAIN] Epoch=1/1, Step=18/38, loss=0.737344, acc1=0.875, acc2=1.0, lr=0.025, time_each_step=0.55s, eta=0:0:13\n",
      "2021-08-14 17:55:40 [INFO]\t[TRAIN] Epoch=1/1, Step=20/38, loss=0.645504, acc1=0.78125, acc2=1.0, lr=0.025, time_each_step=0.52s, eta=0:0:12\n",
      "2021-08-14 17:55:41 [INFO]\t[TRAIN] Epoch=1/1, Step=22/38, loss=0.999565, acc1=0.78125, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:5\n",
      "2021-08-14 17:55:41 [INFO]\t[TRAIN] Epoch=1/1, Step=24/38, loss=1.025367, acc1=0.625, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:4\n",
      "2021-08-14 17:55:42 [INFO]\t[TRAIN] Epoch=1/1, Step=26/38, loss=1.964332, acc1=0.71875, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:4\n",
      "2021-08-14 17:55:42 [INFO]\t[TRAIN] Epoch=1/1, Step=28/38, loss=0.396329, acc1=0.96875, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:3\n",
      "2021-08-14 17:55:43 [INFO]\t[TRAIN] Epoch=1/1, Step=30/38, loss=0.379909, acc1=0.84375, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:3\n",
      "2021-08-14 17:55:43 [INFO]\t[TRAIN] Epoch=1/1, Step=32/38, loss=0.610344, acc1=0.78125, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:2\n",
      "2021-08-14 17:55:44 [INFO]\t[TRAIN] Epoch=1/1, Step=34/38, loss=0.72252, acc1=0.75, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:2\n",
      "2021-08-14 17:55:44 [INFO]\t[TRAIN] Epoch=1/1, Step=36/38, loss=0.469584, acc1=0.84375, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:1\n",
      "2021-08-14 17:55:45 [INFO]\t[TRAIN] Epoch=1/1, Step=38/38, loss=0.847508, acc1=0.75, acc2=1.0, lr=0.025, time_each_step=0.26s, eta=0:0:1\n",
      "2021-08-14 17:55:45 [INFO]\t[TRAIN] Epoch 1 finished, loss=0.865143, acc1=0.752467, acc2=1.0, lr=0.025 .\n",
      "2021-08-14 17:55:45 [INFO]\tStart to evaluating(total_samples=138, total_steps=5)...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  0%|          | 0/5 [00:00<?, ?it/s]share_vars_from is set, scope is ignored.\n",
      "100%|██████████| 5/5 [00:02<00:00,  1.86it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-08-14 17:55:47 [INFO]\t[EVAL] Finished, Epoch=1, acc1=0.478261, acc2=1.0 .\n",
      "2021-08-14 17:55:49 [INFO]\tModel saved in output/DenseNet1612/best_model.\n",
      "2021-08-14 17:55:50 [INFO]\tModel saved in output/DenseNet1612/epoch_1.\n",
      "2021-08-14 17:55:50 [INFO]\tCurrent evaluated best model in eval_dataset is epoch_1, acc1=0.4782608695652174\n"
     ]
    }
   ],
   "source": [
    "model.train(num_epochs=1,\r\n",
    "            train_dataset=train_dataset,\r\n",
    "            train_batch_size=32,\r\n",
    "            eval_dataset=eval_dataset,\r\n",
    "            lr_decay_epochs=[1,2,3],\r\n",
    "            save_interval_epochs=1,\r\n",
    "            learning_rate=0.025,\r\n",
    "            pretrain_weights=None,\r\n",
    "            save_dir='output/DenseNet1612',\r\n",
    "            use_vdl=True,\r\n",
    "            early_stop=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 4.模型预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-08-14 18:05:38 [INFO]\tModel[DenseNet161] loaded.\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAHHZJREFUeJztnUuSJDmanBWwl7vHKx9V1Rw2h7MgZc5D4YLX4Cl4Pt5ghGTPdE1VV1VWZPjDXgC4yKUqhNG98BEh9FsiEeYwwP6EiELx/6GUAmNMe8R/6wEYY/5tcPAb0ygOfmMaxcFvTKM4+I1pFAe/MY3i4DemURz8xjSKg9+YRunv+WP/5b/9V7ITdukfZd9TfKK269M/yb5/Ov0vavt6nWTf6fxHavsB38u+H4+B2vZp4XENF/n318OP1JbHL7LvsfBvTfOD7Lv/xu82X2bZF5nH9hB32fWhP1FbwYEfueo9Y9xv1Db0Z9l3P3L7tdOf43I7Utt6/YN+Lp6pLR95fffHTv49HnhuDsdfZdex5757/I+y7+/9D9T2Om6ybxl4fU9R9+0nHtv//O//gz8mgXd+YxrFwW9Mozj4jWkUB78xjXJXwe84syg15N9k36Fn8eh8fZV9887PDYn/HgAwZmraJi1Kzd1Hfu7KUxbWq/z7qfC4IlbZ9xhGahsyjxUA1sDPTZWlzDsLYH3Q79tDCJcjX/nOBx4rAPQrz3lfKr+VErWVfZB9953FuQ5a4Owii4Mh8vqk7jv591vPYluXf5F948oCKUYtJOaJ1z0/Vt634zmfywfZd4gsQL8X7/zGNIqD35hGcfAb0ygOfmMaxcFvTKPcVe0ff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7q8zlwnXYACBt/A6XojPyioMB9JUacePA6vPDQSvwJ6H2L6NWr8vESnUOnJAkFV0fLgjLbRgqFtbEyn7Nqfokkltc90rGZfFJR2Ex3nv9DmVgb22ctEW5iJqFQ2V9y1VYhDf9winxO9Q0/coBybvwzm9Mozj4jWkUB78xjeLgN6ZRQinaFmqM+f8b7/zGNIqD35hGcfAb0ygOfmMaxcFvTKM4+I1pFAe/MY3i4DemURz8xjSKg9+YRnHwG9MoDn5jGsXBb0yjOPiNaRQHvzGN4uA3plEc/MY0ioPfmEZx8BvTKA5+YxrFwW9Mozj4jWkUB78xjfJ/Ab0EES0hWhbtAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Predict Result: [{'category_id': 0, 'category': 'N0_NPY', 'score': 0.99974436}]\n"
     ]
    }
   ],
   "source": [
    "model = pdx.load_model('output/DenseNet1612/best_model')\r\n",
    "image_name = 'ttst/N0_NPY/N0_19_29.npy'\r\n",
    "result = model.predict(image_name)\r\n",
    "img1 = np.load(image_name)\r\n",
    "plt.imshow(img1[:,:,1:])\r\n",
    "plt.axis('off')\r\n",
    "plt.show()\r\n",
    "print(\"Predict Result:\", result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 四、效果展示  \n",
    "  \n",
    " * 这里我训练了9种模型，并对其进行了对比，结果如下：（由于数据量有点少，不过多评价）\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/f1f61cf4719c4966befee0a981ba4e8a9265db4829124c249572239837b76384)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 五、连通域获取  \n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/88af9e283eb045619273dd49e9294507a1e000202b3d475d97cef9bbd5ad7e10)\n",
    "```python\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "%matplotlib inline\n",
    "w, h = timg.shape\n",
    "color = []\n",
    "color.append((0, 0, 0))\n",
    "img_color = np.zeros((w, h, 3), dtype=np.uint8)\n",
    "retval, labels, stats, centroids = cv2.connectedComponentsWithStats(timg)\n",
    "for num in range(1,retval):\n",
    "    color_b = random.randint(0, 255)\n",
    "    color_g = random.randint(0, 255)\n",
    "    color_r = random.randint(0, 255)\n",
    "    color.append((color_b, color_g, color_r))\n",
    "for x in range(w):\n",
    "    for y in range(h):\n",
    "        lable = labels[x,y]\n",
    "        img_color[x,y,:] = color[int(lable)]\n",
    "\n",
    "plt.imshow(img_color)\n",
    "#plt.savefig('./connect.png', dpi=300,bbox_inches='tight')\n",
    "plt.show()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 六、总结与升华 \n",
    "* 这个项目是从登陆社区以来一直在做的项目，遇到过一些困难，最终也是顺利解决了困难；  \n",
    "* 对于遥感影像来说，最难的就是读取影像与选择模型了，因为大部分模型在卷积的第一层都不匹配，所以加载预训练模型就要修改网络；  \n",
    "* 非常感谢飞桨提供的学习交流的平台，使得我能学习到很多的知识，当然这个项目还有许多需要完善的地方，希望能与大家多交流。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 个人简介  \n",
    "* 对深度学习、机器学习、强化学习都有少许了解，里面有太多有趣的知识等着我去发现；  \n",
    "* 本人来自华中农业大学地理信息科学专业，准大三，希望与大家能多多交流。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.1.2 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
